#include "Arduino.h"
#include <ArduinoJson.h>
#include "WiFi.h"
#include <WebServer.h>
#include <WiFiManager.h>
#include "Audio.h"
#include <SPI.h>
#include <TFT_eSPI.h>
#include "background.h"
#include "FS.h"
#include "SPIFFS.h"
#include "SettingPage.h"
#include "Orbitron_Medium_20.h"
#include <U8g2_for_TFT_eSPI.h>

//#define BOARD_Tdisplay 1
//#define BOARD_TFT_AUDIO_202409  1   //202409 TFT Audio 小板
//#define BOARD_TFT_AUDIO_202504  1
//#define BOARD_Micropython 1
//#define BOARD_Esp32S2Mini 1
#define BOARD_Esp32_tft1_9_1387 1



#define NTP1 "ntp1.aliyun.com"
#define NTP2 "ntp2.aliyun.com"
#define NTP3 "ntp3.aliyun.com"
#define PINKeyboard 39
#define PINKeyboard2 1

// TTGO T-display
#ifdef BOARD_Tdisplay
#define I2S_LRC     32
#define I2S_BCLK    33
#define I2S_DOUT    25
#endif

//micropython 板
#ifdef BOARD_Micropython
#define I2S_LRC     25 //21 // 37 //32
#define I2S_BCLK    26 //22 // 35 //33
#define I2S_DOUT    27 //35 // 33 //25
#endif

//Touch keys definition
#define Touch1 14
#define Touch2 12
#define Touch3 32
#define Touch4 33

#ifdef BOARD_TFT_AUDIO_202409
//MAX 98357
  #define I2S_LRC     27 //21 // 37 //32
  #define I2S_BCLK    26 //22 // 35 //33
  #define I2S_DOUT    25 //35 // 33 //25

//Touch Input
  #define Touch1 14
  #define Touch2 12
  #define Touch3 32
  #define Touch4 33
#endif



#ifdef BOARD_TFT_AUDIO_202504
//MAX 98357
  #define I2S_LRC     27 //21 // 37 //32
  #define I2S_BCLK    26 //22 // 35 //33
  #define I2S_DOUT    25 //35 // 33 //25

//Touch Input
  #define Touch1 13
  #define Touch2 15
  #define Touch3 14
  #define Touch4 12
#endif

#ifdef BOARD_Esp32_tft1_9_1387
//tda 1387
#define I2S_LRC     26 
#define I2S_BCLK    25 
#define I2S_DOUT    27 

//Touch Input
  #define Touch1 13
  #define Touch2 15
  #define Touch3 14
  #define Touch4 12
#endif


int BtnT1 = 10000;                 
int BtnT2 = 10000;
int BtnT3 = 10000;
int BtnT4 = 10000;

//esp32 s2 mini
#ifdef BOARD_Esp32S2Mini
#define I2S_LRC     37 
#define I2S_BCLK    35 
#define I2S_DOUT    33 
#endif

#define HanziUI 1   //显示汉字UI
#define UIx0 5
#define UIx1 10
#define UIx2 78
#define UIx3 120
#define UIx4 160

#define UIRow1 25
#define UIRow2 50
#define UIRow3 75
#define UIRow4 100
#define UIRow5 135


const int BTNA = 0;  // GPIO Play and Pause
#ifdef  BOARD_Esp32S2Mini
  const int BTNB = 0;  //35
#else
  const int BTNB = 35;
#endif

//const int BTNC = 12;
//const int BTND = 17;

#define numCh 20
#define TFT_GREY 0x5AEB // New colour
#define Color_RED1 0x8000
#define Color_BLUE1 0x1A6F
#define Color_YELLOW1 0xDDAE

int stOrd=0;
Audio audio;
//Audio audio(true, I2S_DAC_CHANNEL_BOTH_EN);  //内部DAC

U8g2_for_TFT_eSPI  u8g2;
TFT_eSPI tft = TFT_eSPI();

//String ssid =    "ChinaNet-yu";
//String password = "ydd200706";

const char* ssid =    "EspWebServerSetupAP";
const char* password = "";

WebServer server(80);

String stations= \
"清晨音乐,https://lhttp.qtfm.cn/live/4915/64k.mp3?app_id=web\n \
年代音乐959,https://lhttp.qtfm.cn/live/5021381/64k.mp3?app_id=web\n \
北京文艺,https://lhttp.qingting.fm/live/333/64k.mp3?app_id=web\n  \ 
中国之声,https://lhttp.qingting.fm/live/20500102/64k.mp3?app_id=web \n \
北京新闻,https://lhttp.qingting.fm/live/339/64k.mp3?app_id=web\n \
CFPL,http://live.leanstream.co/CFPLAM-MP3\n \
哈尔滨音乐,http://lhttp.qingting.fm/live/5022338/64k.mp3\n \
北京音乐,https://lhttp.qingting.fm/live/332/64k.mp3\n \
北京交通,https://lhttp-hw.qtfm.cn/live/336/64k.mp3?app_id=web\n \
广东文体,https://lhttp-hw.qtfm.cn/live/471/64k.mp3?app_id=web\n \
广东新闻,https://lhttp-hw.qtfm.cn/live/1254/64k.mp3?app_id=web\n \
广东城市之声,https://lhttp-hw.qtfm.cn/live/469/64k.mp3?app_id=web\n \
上海流行音乐,https://lhttp-hw.qtfm.cn/live/273/64k.mp3?app_id=web\n \
第一财经,https://lhttp-hw.qtfm.cn/live/276/64k.mp3?app_id=web\n \
上海沸点100,https://lhttp-hw.qtfm.cn/live/5022341/64k.mp3?app_id=web\n \
长春交通之声,https://lhttp-hw.qtfm.cn/live/4967/64k.mp3?app_id=web\n \
CFPLAM,http://live.leanstream.co/CFPLAM-MP3\n \
CHML,http://live.leanstream.co/CHMLAM-MP3\n \
CHQR,http://live.leanstream.co/CHQRAM\n \
CFLN,http://live.leanstream.co/CFLNFM\n";

// char * stationArr=stations._cstr();

// char * arrayURL[numCh] = {
//   "https://lhttp.qtfm.cn/live/4915/64k.mp3?app_id=web",
//   "https://lhttp.qtfm.cn/live/5021381/64k.mp3?app_id=web",
//   "https://lhttp.qingting.fm/live/333/64k.mp3?app_id=web",
//   "https://lhttp.qingting.fm/live/20500102/64k.mp3?app_id=web",
//   "https://lhttp.qingting.fm/live/339/64k.mp3?app_id=web",
//   "http://live.leanstream.co/CFPLAM-MP3",
//   "http://lhttp.qingting.fm/live/5022338/64k.mp3",
//   "https://lhttp.qingting.fm/live/332/64k.mp3",
//   "http://live.leanstream.co/CFPLAM-MP3",
//   "http://live.leanstream.co/CHMLAM-MP3",
//   "http://live.leanstream.co/CHQRAM",
//   "http://live.leanstream.co/CFLNFM",
// };

// char * arrayStation[numCh]={
//   "Morning Music",
//   "Vintage music 959",
//   "Beijing Culture",
//   "Voice of China",
//   "Beijing news",
//   "CFPL",
//   "Harbin Music",
//   "Beijing Music",
//   "CFPL",
//   "CHML",
//   "CHQR",
//   "CFLNFM"
// };
//"北京文艺","中国之声","北京新闻","CFPL","哈尔滨音乐"

String URL="";       //当前电台URL
String station="";  //当前电台名称
int volume=50;  //初始音量 50
struct tm timeInfo;
int loopCounter=0;

void getStation(int index)
{
  //return arrayStation[index];
  String foundStr = findString(stations,index);
  if (foundStr.length()>0){
    int commapos = foundStr.indexOf(",",0);
    if(commapos>0){
      station=foundStr.substring(0,commapos);
      station.trim();
      int posCR = foundStr.indexOf("\n",0);
      if(posCR<0) 
        URL = foundStr.substring(commapos+1); 
      else
        URL = foundStr.substring(commapos+1,posCR);
    }
  }
}

//查找第index 行，用\n分割； 
#define EOL '\n'
String findString(String s,int index)
{
  bool Eof=false;
  int i=-1;
  int stPos=0;
  String subs="";
  while (!Eof && i<index)
  {
    i++;
    int ifound=s.indexOf(EOL,stPos);
    if(ifound>=0){
        subs=s.substring(stPos,ifound);
        stPos=ifound+1;
        if(stPos>=s.length()) Eof=true;
    }
    else{
        Eof=true;
        subs=s.substring(stPos);
    }
  }
  return subs;
}
//文件名
char * fn = "stations.txt";
void saveStations()
{
  File file = SPIFFS.open(fn,FILE_WRITE);
  file.print(stations);
  file.close();
  Serial.println("Saved to file: stations.txt");
}
void setup() {
  Serial.begin(115200);
  //--display 
  tft.init();
  tft.setTextFont(2);
  u8g2.begin(tft);
  tft.setRotation(1);  //0:竖屏，1:横屏
  tft.setSwapBytes(true);
  u8g2.setFont(u8g2_font_wqy16_t_gb2312);
  clrscr();

  //挂载文件系统
  if (SPIFFS.begin(true))
  {
    printBoth("SPIFFS file system mounted successfuly!");
  }
  //读取文件
  
  if(!SPIFFS.exists(fn))
  {
    saveStations();
  }
  else
  {
    File file = SPIFFS.open(fn,FILE_READ);
    if(file)
    {
      stations=file.readString();
      file.close();
      printBoth("Reading from file stations.txt:");
      printBoth(stations);
    }
  }

  
//--wifi
  //initwifi();
  printBoth("Connecting to wifi. If this info is still here after 60 seconds, please refer to the manual to configure WiFi.");
  WiFiManager wm;
 bool response = wm.autoConnect(ssid, password);
  if (!response) {
    printBoth("Failed to connect. Retrying connection ...");
    //Try again in 5s.
    ESP.restart();
    delay(5000);
  } 
  else 
  {
    printBoth("Connected to wifi. ");
    printBoth(WiFi.localIP().toString());
  }
  initWebServer();
  //configTime(8*3600,0,NTP1,NTP2,NTP3);
  delay(1000);
  
  //showUI();

// //Wifi
//   WiFi.disconnect();
//   WiFi.mode(WIFI_STA);
//   WiFi.begin(ssid.c_str(), password.c_str());
//   while (WiFi.status() != WL_CONNECTED)
//   delay(1000);
//   Serial.println("Wifi connected.");
  clrscr();
  showUI1();
  //showUI4();

  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(volume);
  getStation(0);
  //audio.connecttohost("http://vis.media-ice.musicradio.com/CapitalMP3");
  audio.connecttohost("https://lhttp.qingting.fm/live/333/64k.mp3?app_id=web");
  //audio.connecttohost(URL.c_str());
  Serial.println("Playing...");

  //显示IP
  u8g2.setFont(u8g2_font_wqy12_t_gb2312);
  u8g2.setForegroundColor(TFT_GREEN);  //字的颜色
  drawString(WiFi.localIP().toString().c_str(),UIx4,UIRow5);
  //设回字体
  u8g2.setFont(u8g2_font_wqy16_t_gb2312);

  // pinMode(BTNA, INPUT);
  // pinMode(BTNB, INPUT);
  // pinMode(BTNC, INPUT_PULLUP);
  // pinMode(BTND, INPUT_PULLUP);
}

void clrscr()
{
  tft.fillRect(0, 0, TFT_WIDTH,TFT_HEIGHT, TFT_BLACK);
}

void printBoth(String str)
{
  Serial.println(str);
  tft.println(str);
}

// void showUI()
// {
//   tft.setFreeFont(&Orbitron_Medium_20);
//   tft.fillScreen(TFT_BLACK);
//   tft.pushImage(0, 0,  135, 240, background);

//   tft.setCursor(14, 20);
//   tft.println("Radio");
//   tft.drawLine(0,28,135,28,TFT_GREY);
  
//   // for(int i=0;i<b+1;i++)
//   //   tft.fillRect(108+(i*4),18,2,6,TFT_GREEN);

//   tft.drawString("Ready   ",78,44,2);
//   tft.drawString(String(volume),78,66,2 );
//   tft.drawString(String(arrayStation[stOrd]),12,108,2);
//   tft.setTextFont(1);
//   tft.setCursor(8, 211, 1);
//   tft.println(WiFi.localIP());
//   Serial.printf("STATUS(System) Ready \n\n");
// }

// void showUI3()
// {
//   tft.setFreeFont(&Orbitron_Medium_20);
//   tft.fillScreen(TFT_BLACK);
//   //tft.pushImage(0, 0,  135, 240, background);
//   tft.setCursor(14, 20);
//   tft.println("Radio");
//   tft.drawLine(0,28,135,28,TFT_GREY);
//   delay(1000);
//   // for(int i=0;i<b+1;i++)
//   //   tft.fillRect(108+(i*4),18,2,6,TFT_GREEN);

//   tft.drawString("Ready   ",78,44,2 );
//   tft.drawString(String(volume),78,66,2 );
//   tft.drawString(String(arrayStation[stOrd]),12,108,2);
//   tft.setTextFont(1);
//   tft.setCursor(8, 211, 1);
//   tft.println(WiFi.localIP());
//   Serial.printf("STATUS(System) Ready \n\n");
// }

void showUI1()
{
  tft.setFreeFont(&Orbitron_Medium_20);
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(5,20);
  tft.drawString("Bruce Radio",10,0);

  u8g2.setForegroundColor(TFT_WHITE);  //字的颜色
  u8g2.setFont(u8g2_font_wqy16_t_gb2312);
  //u8g2.setTextSize(2);
  //u8g2.setFont(u8g2_font_tenthinguys_tu); 
  // u8g2.setCursor(5,20);
  // u8g2.print("Bruce Radio.");

  //u8g2.setFont(u8g2_font_7x14B_tf);
  tft.drawLine(0,28,240,28,TFT_GREY);
  //u8g2.setTextSize(1);
  
  
  tft.fillRect(0,35,60,20,Color_BLUE1);
  u8g2.setForegroundColor(TFT_WHITE);
  u8g2.setBackgroundColor(Color_BLUE1);
  u8g2.drawUTF8(UIx0,51,"Status:");

  tft.fillRect(0,60,60,20,Color_RED1);
  u8g2.setForegroundColor(TFT_WHITE);
  u8g2.setBackgroundColor(Color_RED1);
  u8g2.drawUTF8(UIx0,76,"Gain:");

  
  u8g2.setBackgroundColor(TFT_BLACK);
  u8g2.setForegroundColor(Color_YELLOW1);
  u8g2.drawUTF8(UIx0,UIRow4,"Playing:");
  
  //u8g2.setFont(u8g2_font_wqy16_t_gb2312);
  u8g2.setForegroundColor(TFT_WHITE);
  u8g2.drawUTF8(UIx2,UIRow4,"北京文艺台");

  //u8g2.setForegroundColor(TFT_GREY);
  //u8g2.drawUTF8(5,135,"192.168.1.43");
  //u8g2.drawUTF8(200,28,"13:45");

}

//无图片横屏英文
// void showUI4()
// {
  
// //  tft.setFreeFont(&Orbitron_Medium_20);
//   tft.fillScreen(TFT_BLACK);
//   tft.setCursor(5,20);
//   drawString("Bruce Radio",10,20);

//   //tft.setTextFont(2);
//   //u8g2.setFont(u8g2_font_7x14B_tf);
//   tft.drawLine(0,28,240,28,TFT_GREY);
//   //u8g2.setTextSize(1);
    
//   tft.fillRect(0,35,60,20,Color_BLUE1);
//   u8g2.setBackgroundColor(Color_BLUE1);
//   //tft.setTextColor(TFT_WHITE,Color_BLUE1);
//   drawString("Status :",UIx1,50);

//   tft.fillRect(0,60,60,20,Color_RED1);
//   //tft.setTextColor(TFT_WHITE,Color_RED1);
//   u8g2.setBackgroundColor(Color_RED1);
//   drawString("Gain :",UIx1,75);

//   u8g2.setForegroundColor(TFT_WHITE);  //字的颜色
//   u8g2.setBackgroundColor(TFT_BLACK);
//   drawString("Playing :",UIx1,UIRow4);
//   drawString("北京音乐",UIx2,UIRow4);
//   //tft.drawString("Beijing Music",100,90);

//   //tft.setTextColor(TFT_GREY,TFT_BLACK);
//   //u8g2.drawUTF8(5,135,"192.168.1.43");
//   //tft.drawString("13:46",200,20);

// //显示状态
//   drawString("Ready",UIx2,50);
//   drawString(station.c_str(),UIx2,90);
  
//   //tft.setTextFont(1);
  
//   Serial.printf("STATUS(System) Ready \n\n");
// }

void drawString(const char* str,int x,int y)
{
  #ifdef HanziUI
    #define TEXTYOFFSET 10
    Serial.printf("drawString u8g2: %s,%d,%d",str,x,y);
    u8g2.drawUTF8(x,y,str);
    
    //u8g2.setCursor(x,y+TEXTYOFFSET); //位置
    //u8g2.print(str); //打
  #else
    Serial.printf("drawString tft: %s,%d,%d",str,x,y);
    tft.drawString(str,x,y);
  #endif
}

void drawStringClearBk(String str,int x,int y,int color)
{
  Serial.printf("drawStringClearBK:%s,%d,%d:\n\r",str,x,y);
  tft.fillRect(x,y-15,100,20,color);
  drawString(str.c_str(),x,y);
}

int readAnalogV(int pinIn)
{
  int anaV = 0;
//消除抖动
  int av1=analogRead(pinIn);
  //delay(50);
  int av2=analogRead(pinIn);
  //delay(50);
  int av3=analogRead(pinIn);

  if( abs(av1-av2)<50 && abs(av2-av3)<50 && av1>50 && av2>50 && av3>50)
  {
    anaV=(av1+av2+av3)/300;
    if(anaV>5)
    {
    // Serial.print("analogV:");
    // Serial.print(pinIn);
    // Serial.print("-");
    // Serial.println(anaV);
    }
  }
  return anaV;
}

int runningC=0;

   
void loop()
{
  int analogV=10000;

  #ifdef Touch1
    BtnT1 = touchRead(Touch1);                 
    BtnT2 = touchRead(Touch2);
    BtnT3 = touchRead(Touch3);
    BtnT4 = touchRead(Touch4);
    //Serial.printf("T1:%d,T2:%d,T3:%d,T4:%d",BtnT1,BtnT2,BtnT3,BtnT4);
  #else
    analogV = readAnalogV(PINKeyboard);
  #endif



  if(BtnT1<50 || analogV==28 || analogV==29){
  //if(BtnT2<50){
    volume=(volume+1)%100;
    audio.setVolume(volume);
    //drawString(String(volume)+"     ",UIx2,UIRow3,TFT_BLACK );
    u8g2.drawUTF8(UIx2,UIRow3,(String(volume)+"     ").c_str());
    Serial.printf("Volume Changed:%d \n\r",volume);
    delay(200);
  }
  //Volume -
  //if(analogV==25 || analogV==26){
  if(BtnT2<50 || analogV==25 || analogV==26){
    volume=volume-1;
  // //if(BtnT13<50){
  //   if(BtnT13>30)
  //     volume=volume-1;
  //   else 
  //     volume=volume+1;

    if(volume<0) volume=0;
    audio.setVolume(volume);
    drawStringClearBk(String(volume)+"     ",UIx2,UIRow3,TFT_BLACK );
    Serial.printf("Volume Changed:%d \n\r",volume);
    delay(200);
  }

  //Serial.printf("AnalogV:%d,AnalogV2:%d",analogV,analogV2);
  //Serial.printf("BTNB:%d",digitalRead(BTNB));
  //按钮B，切换 Station

  int stationChange=getStationChange(analogV);
  if(stationChange!=0)
  {
    
        stOrd = (stOrd + stationChange) % numCh;
        if(stOrd<0) stOrd=numCh-1;
  //   else
  //   {
  //     stOrd = stOrd-1;
  //     if (stOrd<0) stOrd=numCh-1;
  //   }
      //URL = getStationUrl(stOrd);
      Serial.println("Station changed:"+station);
      getStation(stOrd);   
  //   tft.drawString(String(station)+"           ",UIx2,90,2);

  //   //too big to use chinese characters.
      u8g2.setForegroundColor(TFT_WHITE);
      u8g2.setBackgroundColor(TFT_BLACK);
      drawStringClearBk(station+"      ",UIx2,UIRow4,TFT_BLACK);
      
      Serial.println(station);
      Serial.println(URL);
      audio.connecttohost(URL.c_str());
      delay(200);
  }
    audio.loop();
    server.handleClient();
}

int getStationChange(int analogV)
{
  int result = 0;
  #ifdef BOARD_Esp32S2Mini
    int anaBtnB4 = analogRead(4);
    if(anaBtnB4>8000) result = 1;
  #endif
  #ifdef BOARD_Tdisplay
  //int analogV = readAnalogV(PINKeyboard);
  //int analogV2 =0; //readAnalogV(PINKeyboard2);
  if (digitalRead(BTNB) ==  LOW || analogV==31 || analogV==32 || analogV==20 || analogV==21)
    if(digitalRead(BTNB) ==  LOW || analogV==31 || analogV==32)
      result = 1;
    else
      result =-1;
//if (analogV2>10 || analogV==31 || analogV==32 || analogV==20 || analogV==21) {
  #endif
  #ifdef BOARD_Micropython
    if(analogRead(PINKeyboard)>1000) result = 1;
    //Serial.printf("touchRead1:%d,touchread2:%d",touchRead(PINKeyboard),touchRead(PINKeyboard2));
  #endif

  #ifdef BOARD_TFT_AUDIO_202409 
    if(BtnT3<50) result=-1;
    if(BtnT4<50) result=1;    
  #endif
  
  #ifdef BOARD_Esp32_tft1_9_1387
    //Serial.printf("Change Station: BOARD_Esp32_tft1_9_1387. Btn3:%d;Btn4:%d",BtnT3,BtnT4);
    if(BtnT3<50) result=-1;
    if(BtnT4<50) result=1;
  #endif
  
    //Serial.printf("getStationChange: %d",result);
  //delay(50);
  return result;
}

void initWebServer() {
  //Serve the HTML page to client when they go to the root path.
  server.on("/", HTTP_GET, serveInitialPage);
  //Setup other endpoints.
  //server.on("/toggleLED", HTTP_GET, toggleLED);
  server.on("/updateStations", HTTP_POST, updateStations);
  //Start web server.
  server.begin();
}

void serveInitialPage() {
  server.send(200, "text/html", processPageVars(String(WEBPAGE)));
}

String processPageVars(String pageContent)
{
  pageContent.replace("@Stations",stations);
  return pageContent;
}

void updateStations()
{
  //Get data from the body of the request.
  //String requestBody = server.arg("plain");
  String requestBody = server.arg(0); //上传的Stations数据
  Serial.println("requestBody:");
  Serial.println(requestBody);
  
  //Deserialize a json string.
  StaticJsonDocument<2000> deserJsonDoc; //Set size of buffer to 200 bytes. For larger payloads DynamicJsonDocument(allocated on the heap) can be used.
  deserializeJson(deserJsonDoc, requestBody); //Desrialize json.
  const char* sensorID = "sensor1";//deserJsonDoc["sensorID"]; //Get value from json document.
  const char* sta1 = deserJsonDoc["txt1"];
  Serial.println(sta1);
  stations=String(sta1);
  saveStations();
  Serial.println("-----------Updated stations:------------");
  Serial.println(stations);
  //Read a sensor based on the sensor ID provided in the json.
  int sensorValue = volume; //返回值为音量

  //返回值 Serialize read value to a json string.
  StaticJsonDocument<200> serJsonDoc; //Set size of buffer to 200 bytes.
  serJsonDoc["value"] = sensorValue; //Set value.
  char jsonString[200]; //Make a json string variable.
  serializeJson(serJsonDoc, jsonString); //Serialize json.

  //Return read value as json.
  server.send(200, "application/json", jsonString); //Send web page
}

// optional
void audio_info(const char *info){
    Serial.print("info        "); Serial.println(info);
}
void audio_id3data(const char *info){  //id3 metadata
    Serial.print("id3data     ");Serial.println(info);
}
void audio_eof_mp3(const char *info){  //end of file
    Serial.print("eof_mp3     ");Serial.println(info);
}
void audio_showstation(const char *info){
    Serial.print("station     ");Serial.println(info);
}
void audio_showstreamtitle(const char *info){
    Serial.print("streamtitle ");Serial.println(info);
}
void audio_bitrate(const char *info){
    Serial.print("bitrate     ");Serial.println(info);
}
void audio_commercial(const char *info){  //duration in sec
    Serial.print("commercial  ");Serial.println(info);
}
void audio_icyurl(const char *info){  //homepage
    Serial.print("icyurl      ");Serial.println(info);
}
void audio_lasthost(const char *info){  //stream URL played
    Serial.print("lasthost    ");Serial.println(info);
}



// void initwifi() {
//   WiFi.disconnect();
//   WiFi.softAPdisconnect(true);
//   WiFi.mode(WIFI_STA);
//   WiFi.begin(ssid, password);

//   int i = 0;
//   while (WiFi.status() != WL_CONNECTED) {
//     Serial.print("STATUS(Connecting to WiFi) ");
//     delay(1000);
//   }
//   Serial.println("OK");
// }
